package com.qd.p2p.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AccountLogHisService;
import com.qdlc.p2p.biz.service.AccountLogService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.model.AccountLogHisModel;
import com.qdlc.p2p.dal.model.AccountLogModel;
import com.qdlc.p2p.dal.model.AccountModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 资金
 *
 * @author xx
 * @version 2.0
 * @since 2014年3月17日
 */
public class AccountAction extends BaseAction<AccountModel> implements ModelDriven<AccountModel> {

    @Resource
    private AccountLogService accountLogService;

    @Resource
    private AccountLogHisService accountLogHisService;

    private User user;

    private Map<String, Object> data;

    /**
     * 资金记录
     */
    @Action(value = "/member/account/log", results = {@Result(name = "log", type = "ftl", location = "/member/account/log.html"),
            @Result(name = "log_firm", type = "ftl", location = "/member_borrow/account/log.html")})
    public String log() throws Exception {
        user = getSessionUser();
        UserCache userCache = UserUtils.getUserCacheByUserId(user.getUserId());
        if (userCache.getUserType() == 3 || userCache.getUserType() == 2) {
            return "log_firm";
        }
        return "log";
    }

    /**
     * 资金记录
     */
    @Action("/member/account/logList")
    public void logList() throws Exception {
        user = getSessionUser();
        AccountLogModel m = (AccountLogModel) paramModel(AccountLogModel.class);
        m.setUserId(user.getUserId());
        PageDataList<AccountLogModel> list = accountLogService.list(m);
        data = new HashMap<String, Object>();
        data.put("data", list);
        printWebJson(getStringOfJpaObj(data));
    }

    /**
     * 历史资金记录
     */
    @Action(value = "/member/account/log_his", results = {@Result(name = "log_his", type = "ftl", location = "/member/account/log_his.html"),
            @Result(name = "log_firm", type = "ftl", location = "/member_borrow/account/log.html"),
            @Result(name = "log", type = "ftl", location = "/member/account/log.html")})
    public String logHis() throws Exception {
        user = getSessionUser();
        UserCache userCache = UserUtils.getUserCacheByUserId(user.getUserId());
        if (userCache.getUserType() == 3 || userCache.getUserType() == 2) {
            return "log_firm";
        }
        if (userCache.getIsImp() == 0)
            return "log";
        return "log_his";
    }

    /**
     * 历史资金记录
     *
     * @return
     */
    @Action("/member/account/logHisList")
    public void logHisList() throws Exception {
        user = getSessionUser();
        AccountLogHisModel m = (AccountLogHisModel) paramModel(AccountLogHisModel.class);
        m.setUser(user);
        PageDataList<AccountLogHisModel> list = accountLogHisService.list(m);
        data = new HashMap<String, Object>();
        data.put("data", list);
        printWebJson(getStringOfJpaObj(data));
    }

}
